<?php
namespace app\admin\controller;
use app\admin\logic\ConfLogic;

/**
 * 系统设置api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/04
 * Time: 16:44
 */
class Conf extends Base {

    public $clid = 1;

    public function __construct(){
        parent::__construct();
        $this->c_logic = new ConfLogic();
    }

    /**
     * 获取巡店设置信息
     * @return \think\response\Json
     */
    public function ajax_get_patrol(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['key','in',['cp_time','cp_desc_use_words']];
        $conf_info = $this->c_logic->get_conf_info($maps);
        $conf_info['cp_time'] = json_decode($conf_info['cp_time'],true);
        $conf_info['cp_desc_use_words'] = json_decode($conf_info['cp_desc_use_words'],true);

        $data['patrol_info'] = $conf_info;
        return json($this->c_logic->get_response(1,'OK',$data));
    }

    /**
     * 修改巡店设置信息
     * @return \think\response\Json
     */
    public function ajax_save_patrol(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $c_data = [];
        //$c_data['cp_time'] = json_encode($params['cp_time'],JSON_UNESCAPED_UNICODE);
        $c_data['cp_desc_use_words'] = json_encode($params['cp_desc_use_words'],JSON_UNESCAPED_UNICODE);

        $r = $this->c_logic->save_conf_info($c_data);
        if($r){
            return json($this->c_logic->get_response(1,'操作成功'));
        }
        return json($this->c_logic->get_response(0,'操作失败'));
    }

    /**
     * 获取工作记录日记账信息
     * @return \think\response\Json
     */
    public function ajax_get_wra(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['key','=','work_recoud_account_type'];
        $conf_info = $this->c_logic->get_conf_info($maps);
        $conf_info['work_recoud_account_type'] = json_decode($conf_info['work_recoud_account_type'],true);

        $data['wrat_info'] = $conf_info;
        return json($this->c_logic->get_response(1,'OK',$data));
    }

    /**
     * 修改工作记录日记账信息
     * @return \think\response\Json
     */
    public function ajax_save_wra(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $c_data = [];
        $c_data['work_recoud_account_type'] = json_encode($params['work_recoud_account_type'],JSON_UNESCAPED_UNICODE);

        $r = $this->c_logic->save_conf_info($c_data);
        if($r){
            return json($this->c_logic->get_response(1,'操作成功'));
        }
        return json($this->c_logic->get_response(0,'操作失败'));
    }

    /**
     * 获取积分信息
     * @return \think\response\Json
     */
    public function ajax_get_integral(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['key','=','integral'];
        $conf_info = $this->c_logic->get_conf_info($maps);
        $conf_info['integral'] = json_decode($conf_info['integral'],true);

        $data['integral_info'] = $conf_info;
        return json($this->c_logic->get_response(1,'OK',$data));
    }

    /**
     * 修改积分信息
     * @return \think\response\Json
     */
    public function ajax_save_integral(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $c_data = [];
        $c_data['integral'] = json_encode($params['integral'],JSON_UNESCAPED_UNICODE);

        $r = $this->c_logic->save_conf_info($c_data);
        if($r){
            //修改学习中心分数
            $cl_data = [];
            $cl_data['per_ti_score'] = $params['integral']['class_learn'];

            $maps = [];
            $maps[] = ['id','=',$this->clid];
            $r = model('class_learn')->force()->save($cl_data,$maps);

            return json($this->c_logic->get_response(1,'操作成功'));
        }
        return json($this->c_logic->get_response(0,'操作失败'));
    }
}